$(document).ready(function () {
    //判断标志，跳转2级页面
    if (CI_TYPE == 'host_database_middleware') {
        var url = ctx + "/kpiContrast" + "/" + CI_TYPE + "/" + areaFlag;
        $('#systemViewIframe').attr('src', url);
    }
    resourceIpName();

    $('.chosen-select').chosen({allow_single_deselect: true});

    //resize the chosen on window resize
    $(window)
        .off('resize.chosen')
        .on('resize.chosen', function () {
            $('.chosen-select').each(function () {
                var $this = $(this);
                $this.next().css({'width': $this.parent().width()});
            })
        }).trigger('resize.chosen');
    //resize chosen on sidebar collapse/expand
    $(document).on('settings.ace.chosen', function (e, event_name, event_val) {
        if (event_name != 'sidebar_collapsed') return;
        $('.chosen-select').each(function () {
            var $this = $(this);
            $this.next().css({'width': $this.parent().width()});
        })
    });

    //当前页面打开窗口的尺寸
    window.onresize = function () {
        changeFrameHeight();
    };
    changeFrameHeight();

    $('#form-field-select-4').on('change', function(e, params) {
        changeFrameHeight();
    });

});

//重置页面高度
function changeFrameHeight() {
    var mainIframeHeight = $(window).height();
    var serchPanalHeight = $('.homecontent').height() + 30;
    var systemViewIframeHeight = mainIframeHeight - serchPanalHeight;
    $('#systemViewIframe').height(systemViewIframeHeight);
}

//资源搜索,弹出列表框
function resourceIpName() {
    $.ajax({
        url: ctx + "/kpiContrast/queryResouceByName",
        type: 'post',
        data: {
            DI_QU:areaFlag
        },
        success: function (data) {
            var list = JSON.parse(data);
            $.each(list, function (index, item) {
                $("select[name='resourceName']").append(
                    "<option value='" + item.EXT_UNIT_ID + "'>" + item.RESOURCENAME + "</option>"
                );
            });

            $('#resourceName').change(function(){
                var extUnitId = $("#resourceName").val();
                chooseKpi(extUnitId);
            });

            if ($('#resourceName')) {
                $('#resourceName').chosen({search_contains: true});
            }

        }
    });
}

//选择KPI
function chooseKpi(EXT_UNIT_ID) {
    $.ajax({
        url: ctx + "/kpiContrast/queryKpi",
        type: 'post',
        data: {extUnitId:EXT_UNIT_ID},
        success: function (data) {
            var list = JSON.parse(data);
            $('#form-field-select-4').find("option").remove();
            var kpiHtml = '';
            $.each(list, function (index, item) {
                kpiHtml += '<option value="' + item.KPI_CODE + '">' + item.KPI_NAME + '</option>';
            });
            $('#form-field-select-4').append(kpiHtml);
            $('#form-field-select-4').trigger('chosen:updated');
        }
    });
}

//查询按钮代参跳转3级页面
$('#searchBtn1').click(function () {
    var unit_id = $("#resourceName").val();//资源名称
    var kpiCode = $("#form-field-select-4").val();//指标名称
    var timeHorizon = $("#timeHorizon").val();//时间范围

    if (unit_id == null || unit_id.length == 0 || unit_id == " ") {
        $(this).dynamicPop('请选择资源!', null, null, 'left');
    } else if (kpiCode == null || kpiCode.length == 0 || kpiCode == " ") {
        $(this).dynamicPop('请选择指标名称!', null, null, 'left');
    } else if (timeHorizon == null || timeHorizon.length == 0 || timeHorizon == " ") {
        $(this).dynamicPop('请选择时间范围!', null, null, 'left');
    } else {
        var url = ctx + "/kpiContrast/" + unit_id + "/" + kpiCode + "/" + timeHorizon;
        $('#systemViewIframe').attr('src', url);
    }
});





